package com.lg.system.role.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lg.system.role.dao.RoleDao;
import com.lg.system.role.entity.Role;
import com.lg.system.role.service.RoleMenuService;
import com.lg.system.role.service.RoleService;
import com.lg.system.user.service.UserRoleService;
import com.lg.utils.StringUtils;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-03-19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {

	
	@Autowired
	UserRoleService userRoleService;
	
	@Autowired
	RoleMenuService roleMenuService;
	
	@Override
	public boolean deleteById(Long roleId) {
		//删除角色钱需要先判断该角色下面是否有人。有人的话需要先删除 人员-角色的关系，然后再删除 菜单-角色的关系
		userRoleService.deleteByRoleId(roleId);
		roleMenuService.deleteByRoleId(roleId);
		return super.deleteById(roleId);
	}

	@Override
	public boolean save(Role role) {
		role.setGmtCreate(new Date());
		return super.insert(role);
	}

	@Override
	public boolean update(Role role) {
		role.setGmtModified(new Date());
		return super.updateById(role);
	}

	@Override
	public Set<String> selectRoleSignByUid(Long userId) {
		Set<String> roleSigns=new HashSet<>();
		List<Long> roleIds=userRoleService.selectRoleIdByUId(userId);
		List<Role> roles=selectBatchIds(roleIds);
		for(Role role:roles){
			if(StringUtils.isNotEmpty(role.getRoleSign())){
				roleSigns.add(role.getRoleSign());
			}
		}
		return roleSigns;
	}

}
